package com.gogowan.agroenterprise.storage;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.TextView;

import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.base.TitleActivity;
import com.gogowan.agroenterprise.contorl.WeakRefAsyncTask;
import com.gogowan.agroenterprise.contorl.WeakRefAsyncTask.AsyncLoadListener;
import com.gogowan.agroenterprise.model.BoxCode;
import com.gogowan.agroenterprise.model.HistoryRecordsRequest;
import com.gogowan.agroenterprise.model.HistoryRecordsResult;
import com.gogowan.agroenterprise.model.LoginResultInfo;
import com.gogowan.agroenterprise.model.Message;
import com.gogowan.agroenterprise.utils.PalUtils;
import com.gogowan.agroenterprise.utils.Utils;
import com.gogowan.agroenterprise.widget.adapter.HistoryAdapter;
import com.gogowan.agroenterprise.widget.xlistview.LoadingLayout;
import com.gogowan.agroenterprise.widget.xlistview.XListView;
import com.gogowan.agroenterprise.widget.xlistview.XListViewUtils;

/**
 * @Description:历史记录(类似Fragment hide show)
 * @author mdp
 * @date 2017年1月5日 下午1:36:17
 */
public class HistoryActivity extends TitleActivity {

	private LoginResultInfo loginResult;
	private boolean isBoxInitialed = false;
	private boolean isGiftInitialed = false;
	private int selectedIndex;
	private TextView zxjlBtn;
	private TextView lhjlBtn;
	// 标志默认显示的是装箱还是装盒 1：装箱 2：装盒
	private int flag = 0;

	// ------ 第一个 列表 ------
	private WeakRefAsyncTask weakRefAsyncTask;
	/**
	 * 首页是0还是1
	 */
	protected int FIRST_PAGE_INDEX = 1;
	/**
	 * 每次加载数量
	 */
	private static final int LOAD_MORE_THRESHOLD = 10;
	// s-handler
	private static final int RELOAD_HDL = 1;
	private static final int NULL_HDL = 2;
	private static final int LOAD_HDL = 3;
	private static final int REFRESH_HDL = 4;
	private static final int ON_LOAD_HDL = 5;
	// s-request
	/**
	 * 刷新
	 */
	private static final int REFRESH_RQT = 0;
	/**
	 * 加载更多
	 */
	private static final int LOAD_MORE_RQT = 1;
	/**
	 * 是否是最后一页(0不是1是)
	 */
	private int isLastPage;
	/**
	 * 下次加载更多时的页数
	 */
	private int pageIndex = FIRST_PAGE_INDEX;
	/**
	 * 加载更多前的数量(即位置Index) XListView的index与position相等 index<=0(-1)时为header
	 */
	// private int lastItemIndex;
	private List<BoxCode> allList = new ArrayList<>();
	private List<BoxCode> tempList = new ArrayList<>();
	// 加载页控件
	private LoadingLayout loadingLayout;
	private XListView xListView;
	private HistoryAdapter absListAdapter;
	private XListView.IXListViewListener iXListViewListener;
	/**
	 * 详情
	 */
	private final int DETAIL_ITT = 0;
	private AsyncLoadListener asyncLoadListener;
	// ====== 第一个列表 ======
	// ------ 第二个列表 ------
	private WeakRefAsyncTask weakRefAsyncTask1;
	/**
	 * 首页是0还是1
	 */
	protected int FIRST_PAGE_INDEX1 = 1;
	/**
	 * 每次加载数量
	 */
	private static final int LOAD_MORE_THRESHOLD1 = 10;
	// s-handler
	private static final int RELOAD_HDL1 = 11;
	private static final int NULL_HDL1 = 12;
	private static final int LOAD_HDL1 = 13;
	private static final int REFRESH_HDL1 = 14;
	private static final int ON_LOAD_HDL1 = 15;
	// s-request
	/**
	 * 刷新
	 */
	private static final int REFRESH_RQT1 = 10;
	/**
	 * 加载更多
	 */
	private static final int LOAD_MORE_RQT1 = 11;
	/**
	 * 是否是最后一页(0不是1是)
	 */
	private int isLastPage1;
	/**
	 * 下次加载更多时的页数
	 */
	private int pageIndex1 = FIRST_PAGE_INDEX1;
	/**
	 * 加载更多前的数量(即位置Index) XListView的index与position相等 index<=0(-1)时为header
	 */
	// private int lastItemIndex1;
	private List<BoxCode> allList1 = new ArrayList<>();
	private List<BoxCode> tempList1 = new ArrayList<>();
	// 加载页控件
	private LoadingLayout loadingLayout1;
	private XListView xListView1;
	private HistoryAdapter absListAdapter1;
	private XListView.IXListViewListener iXListViewListener1;
	/**
	 * 详情
	 */
	private final int DETAIL_ITT1 = 10;
	private AsyncLoadListener asyncLoadListener1;
	// ====== 第二个列表 ======

	@Override
	protected int setContentViewID() {
		return R.layout.activity_history;
	}

	@Override
	protected boolean initData(Intent intent, Bundle savedInstanceState) {
		if (savedInstanceState != null) {
			selectedIndex = savedInstanceState.getInt("selectedIndex", 0);
		}
		flag = intent.getIntExtra("flag", 0);
		loginResult = PalUtils.getLoginResultInfo(getApplicationContext());
		return loginResult != null && flag != 0;
	}

	@Override
	protected void initView() {
		initTitle("包装记录", false);
		zxjlBtn = (TextView) findViewById(R.id.history_box_tv);
		lhjlBtn = (TextView) findViewById(R.id.history_gift_tv);
		zxjlBtn.setOnClickListener(this);
		lhjlBtn.setOnClickListener(this);

		// ------ 第一个列表 ------
		asyncLoadListener = initAsyncListener();
		weakRefAsyncTask = new WeakRefAsyncTask(asyncLoadListener);
		absListAdapter = new HistoryAdapter(this, allList, R.layout.layout_history_item);
		absListAdapter.setType(0);
		xListView = (XListView) findViewById(R.id.history_box_xlv);
		xListView.setAdapter(absListAdapter);
		iXListViewListener = new XListView.IXListViewListener() {

			@Override
			public void onRefresh() {
				requestList(FIRST_PAGE_INDEX, 0);
			}

			@Override
			public void onLoadMore() {
				if (allList == null || allList.size() == 0) {
					loadingLayout.showReload(new LoadingLayout.OnReloadClickListener() {
						@Override
						public void onClick() {
							requestList(FIRST_PAGE_INDEX, 0);
						}
					});
					return;
				}
				requestList(pageIndex + 1, setLastItemId(absListAdapter.getItem(absListAdapter.getCount() - 1)));
			}
		};
		xListView.setXListViewListener(iXListViewListener);
		xListView.setFlag(XListViewUtils.REFRESH_TIME0);
		xListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (position > 0) {
					Intent intent = new Intent(HistoryActivity.this, HistoryDetailsActivity.class);
					intent.putExtra(PalUtils.PACK_TYPE_ITT, 1).putExtra(PalUtils.PACK_MODEL_ITT,
							(BoxCode) absListAdapter.getItem(position - 1));
					startActivity(intent);
				}
			}
		});
		isLastPage = 0;
		onLoad(0);
		// 加载页控件
		loadingLayout = (LoadingLayout) findViewById(R.id.history_box_loadingLayout);
		loadingLayout.setReplacedView(xListView);
		loadingLayout.showLoading();
		// ====== 第一个列表 ======
		// ------ 第二个列表 ------
		asyncLoadListener1 = initAsyncListener1();
		weakRefAsyncTask1 = new WeakRefAsyncTask(asyncLoadListener1);
		absListAdapter1 = new HistoryAdapter(this, allList1, R.layout.layout_history_item);
		absListAdapter1.setType(1);
		xListView1 = (XListView) findViewById(R.id.history_gift_xlv);
		xListView1.setAdapter(absListAdapter1);
		iXListViewListener1 = new XListView.IXListViewListener() {

			@Override
			public void onRefresh() {
				requestList1(FIRST_PAGE_INDEX1, 0);
			}

			@Override
			public void onLoadMore() {
				if (allList1 == null || allList1.size() == 0) {
					loadingLayout1.showReload(new LoadingLayout.OnReloadClickListener() {
						@Override
						public void onClick() {
							requestList1(FIRST_PAGE_INDEX1, 0);
						}
					});
					return;
				}
				requestList1(pageIndex1 + 1, setLastItemId(absListAdapter1.getItem(absListAdapter1.getCount() - 1)));
			}
		};
		xListView1.setXListViewListener(iXListViewListener1);
		xListView1.setFlag(XListViewUtils.REFRESH_TIME1);
		xListView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (position > 0) {
					Intent intent = new Intent(HistoryActivity.this, HistoryDetailsActivity.class);
					intent.putExtra(PalUtils.PACK_TYPE_ITT, 2).putExtra(PalUtils.PACK_MODEL_ITT,
							(BoxCode) absListAdapter1.getItem(position - 1));
					startActivity(intent);
				}
			}
		});
		isLastPage1 = 0;
		onLoad(1);
		// 加载页控件
		loadingLayout1 = (LoadingLayout) findViewById(R.id.history_gift_loadingLayout);
		loadingLayout1.setReplacedView(xListView1);
		loadingLayout1.showLoading();
		// ====== 第二个列表 ======
		if (flag == 1) {
			selectedIndex = 0;
		} else if (flag == 2) {
			selectedIndex = 1;
		}
		selectFragment(selectedIndex);
	}

	@Override
	protected void onClicked(View v) {
		switch (v.getId()) {
		case R.id.history_box_tv:
			// 装箱历史
			selectedIndex = 0;
			selectFragment(selectedIndex);
			break;
		case R.id.history_gift_tv:
			// 礼盒装盒历史
			selectedIndex = 1;
			selectFragment(selectedIndex);
			break;
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (weakRefAsyncTask != null) {
			weakRefAsyncTask.cancel();
		}
		if (weakRefAsyncTask1 != null) {
			weakRefAsyncTask1.cancel();
		}
	}

	@Override
	public void handleMessage(TitleActivity activity, android.os.Message msg) {
		switch (msg.what) {
		// ------ 第一个列表 ------
		case ON_LOAD_HDL:
			onLoad(0);
			break;
		case RELOAD_HDL:
			loadingLayout.showReload(new LoadingLayout.OnReloadClickListener() {
				@Override
				public void onClick() {
					requestList(FIRST_PAGE_INDEX, 0);
				}
			});
			break;
		case NULL_HDL:
			absListAdapter.notifyDataSetChanged();
			loadingLayout.showNull(new LoadingLayout.OnNullClickListener() {
				@Override
				public void onClick() {
					requestList(FIRST_PAGE_INDEX, 0);
				}
			});
			break;
		case LOAD_HDL:
			pageIndex++;
			absListAdapter.addDatas(tempList);
			onLoad(0);
			tempList.clear();
			loadingLayout.hide();
			break;
		case REFRESH_HDL:
			pageIndex = FIRST_PAGE_INDEX;
			absListAdapter.clearDatas();
			absListAdapter.addDatas(tempList);
			onLoad(0);
			tempList.clear();
			loadingLayout.hide();
			break;
		// ====== 第一个列表 ======
		// ------ 第二个列表 ------
		case ON_LOAD_HDL1:
			onLoad(1);
			break;
		case RELOAD_HDL1:
			loadingLayout1.showReload(new LoadingLayout.OnReloadClickListener() {
				@Override
				public void onClick() {
					requestList1(FIRST_PAGE_INDEX1, 0);
				}
			});
			break;
		case NULL_HDL1:
			absListAdapter1.notifyDataSetChanged();
			loadingLayout1.showNull(new LoadingLayout.OnNullClickListener() {
				@Override
				public void onClick() {
					requestList1(FIRST_PAGE_INDEX1, 0);
				}
			});
			break;
		case LOAD_HDL1:
			pageIndex1++;
			absListAdapter1.addDatas(tempList1);
			onLoad(1);
			tempList1.clear();
			loadingLayout1.hide();
			break;
		case REFRESH_HDL1:
			pageIndex1 = FIRST_PAGE_INDEX1;
			absListAdapter1.clearDatas();
			absListAdapter1.addDatas(tempList1);
			onLoad(1);
			tempList1.clear();
			loadingLayout1.hide();
			break;
		// ====== 第二个列表 ======
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("selectedIndex", selectedIndex);
	}

	/**
	 * 选择布局
	 * 
	 * @param selectedIndex
	 */
	private void selectFragment(int selectedIndex) {
		switch (selectedIndex) {
		case 0:
			// 箱码关联历史记录
			findViewById(R.id.history_box_ll).setVisibility(View.VISIBLE);
			findViewById(R.id.history_gift_ll).setVisibility(View.GONE);
			zxjlBtn.setBackgroundResource(R.drawable.record_select_left);
			zxjlBtn.setTextColor(0xffffffff);
			lhjlBtn.setBackgroundResource(R.drawable.record);
			lhjlBtn.setTextColor(getResources().getColor(R.color.ck_text_color_1));
			if (!isBoxInitialed) {
				reload(selectedIndex);
				isBoxInitialed = true;
			}
			break;
		case 1:
			// 礼盒装盒历史记录
			findViewById(R.id.history_box_ll).setVisibility(View.GONE);
			findViewById(R.id.history_gift_ll).setVisibility(View.VISIBLE);
			zxjlBtn.setBackgroundResource(R.drawable.record_left);
			zxjlBtn.setTextColor(getResources().getColor(R.color.ck_text_color_1));
			lhjlBtn.setBackgroundResource(R.drawable.record_select);
			lhjlBtn.setTextColor(0xffffffff);
			if (!isGiftInitialed) {
				reload(selectedIndex);
				isGiftInitialed = true;
			}
			break;
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case DETAIL_ITT:
				// 详情页更改成功
				reload(0);
				break;
			case DETAIL_ITT1:
				// 详情页更改成功
				reload(1);
				break;
			default:
				break;
			}
		}
	}

	/**
	 * 初始化异步回调
	 */
	private AsyncLoadListener initAsyncListener() {
		return new AsyncLoadListener() {

			@Override
			public void onResult(AsyncLoadListener asyncLoadListener, String response, int actionId) {
				if (isAlreadyDestroyed()) {
					return;
				}
				if (PalUtils.isEmpty(response)) {
					if (allList == null || allList.size() == 0) {
						getWeakRefHandler().sendEmptyMessage(RELOAD_HDL);
					}
					getWeakRefHandler().sendEmptyMessage(ON_LOAD_HDL);
				} else {
					List<BoxCode> responseModelList = setResponseModelList(response);
					switch (actionId) {
					case REFRESH_RQT:
						if (!PalUtils.isEmpty(responseModelList)) {
							tempList = responseModelList;
							isLastPage = (tempList == null || tempList.size() < LOAD_MORE_THRESHOLD) ? 1 : 0;
							if (tempList == null || tempList.size() == 0) {
								if (allList == null || allList.size() == 0) {
									getWeakRefHandler().sendEmptyMessage(NULL_HDL);
								}
								getWeakRefHandler().sendEmptyMessage(ON_LOAD_HDL);
							} else {
								new Thread(new Runnable() {
									@Override
									public void run() {
										if (xListView.getFooterState() == true) {
											// lastItemIndex = allList.size();
											while (xListView
													.getScrollState() != AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
											}
											getWeakRefHandler().sendEmptyMessage(LOAD_HDL);
										} else {
											XListViewUtils.saveRefreshTime(getApplicationContext(),
													XListViewUtils.REFRESH_TIME0);
											while (xListView
													.getScrollState() != AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
											}
											getWeakRefHandler().sendEmptyMessage(REFRESH_HDL);
										}
									}
								}).start();
							}
						} else {
							isLastPage = 1;
							getWeakRefHandler().sendEmptyMessage(NULL_HDL);
						}
						break;
					case LOAD_MORE_RQT:
						if (!PalUtils.isEmpty(responseModelList)) {
							tempList = responseModelList;
							isLastPage = (tempList == null || tempList.size() < LOAD_MORE_THRESHOLD) ? 1 : 0;
							if (tempList == null || tempList.size() == 0) {
								if (allList == null || allList.size() == 0) {
									getWeakRefHandler().sendEmptyMessage(NULL_HDL);
								}
								getWeakRefHandler().sendEmptyMessage(ON_LOAD_HDL);
							} else {
								new Thread(new Runnable() {
									@Override
									public void run() {
										if (xListView.getFooterState() == true) {
											// lastItemIndex = allList.size();
											while (xListView
													.getScrollState() != AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
											}
											getWeakRefHandler().sendEmptyMessage(LOAD_HDL);
										} else {
											XListViewUtils.saveRefreshTime(getApplicationContext(),
													XListViewUtils.REFRESH_TIME0);
											while (xListView
													.getScrollState() != AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
											}
											getWeakRefHandler().sendEmptyMessage(REFRESH_HDL);
										}
									}
								}).start();
							}
						} else {
							isLastPage = 1;
							getWeakRefHandler().sendEmptyMessage(ON_LOAD_HDL);
							if (allList == null || allList.size() == 0) {
								getWeakRefHandler().sendEmptyMessage(NULL_HDL);
							}
						}
						break;
					default:
						break;
					}
				}
			}

		};
	}

	/**
	 * 初始化异步回调
	 */
	private AsyncLoadListener initAsyncListener1() {
		return new AsyncLoadListener() {

			@Override
			public void onResult(AsyncLoadListener asyncLoadListener, String response, int actionId) {
				if (isAlreadyDestroyed()) {
					return;
				}
				if (PalUtils.isEmpty(response)) {
					if (allList1 == null || allList1.size() == 0) {
						getWeakRefHandler().sendEmptyMessage(RELOAD_HDL1);
					}
					getWeakRefHandler().sendEmptyMessage(ON_LOAD_HDL1);
				} else {
					List<BoxCode> responseModelList = setResponseModelList(response);
					switch (actionId) {
					case REFRESH_RQT1:
						if (!PalUtils.isEmpty(responseModelList)) {
							tempList1 = responseModelList;
							isLastPage1 = (tempList1 == null || tempList1.size() < LOAD_MORE_THRESHOLD1) ? 1 : 0;
							if (tempList1 == null || tempList1.size() == 0) {
								if (allList1 == null || allList1.size() == 0) {
									getWeakRefHandler().sendEmptyMessage(NULL_HDL1);
								}
								getWeakRefHandler().sendEmptyMessage(ON_LOAD_HDL1);
							} else {
								new Thread(new Runnable() {
									@Override
									public void run() {
										if (xListView1.getFooterState() == true) {
											// lastItemIndex1 = allList1.size();
											while (xListView1
													.getScrollState() != AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
											}
											getWeakRefHandler().sendEmptyMessage(LOAD_HDL1);
										} else {
											XListViewUtils.saveRefreshTime(getApplicationContext(),
													XListViewUtils.REFRESH_TIME1);
											while (xListView1
													.getScrollState() != AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
											}
											getWeakRefHandler().sendEmptyMessage(REFRESH_HDL1);
										}
									}
								}).start();
							}
						} else {
							isLastPage1 = 1;
							getWeakRefHandler().sendEmptyMessage(NULL_HDL1);
						}
						break;
					case LOAD_MORE_RQT1:
						if (!PalUtils.isEmpty(responseModelList)) {
							tempList1 = responseModelList;
							isLastPage1 = (tempList1 == null || tempList1.size() < LOAD_MORE_THRESHOLD1) ? 1 : 0;
							if (tempList1 == null || tempList1.size() == 0) {
								if (allList1 == null || allList1.size() == 0) {
									getWeakRefHandler().sendEmptyMessage(NULL_HDL1);
								}
								getWeakRefHandler().sendEmptyMessage(ON_LOAD_HDL1);
							} else {
								new Thread(new Runnable() {
									@Override
									public void run() {
										if (xListView1.getFooterState() == true) {
											// lastItemIndex1 = allList1.size();
											while (xListView1
													.getScrollState() != AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
											}
											getWeakRefHandler().sendEmptyMessage(LOAD_HDL1);
										} else {
											XListViewUtils.saveRefreshTime(getApplicationContext(),
													XListViewUtils.REFRESH_TIME1);
											while (xListView1
													.getScrollState() != AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
											}
											getWeakRefHandler().sendEmptyMessage(REFRESH_HDL1);
										}
									}
								}).start();
							}
						} else {
							isLastPage1 = 1;
							getWeakRefHandler().sendEmptyMessage(ON_LOAD_HDL1);
							if (allList1 == null || allList1.size() == 0) {
								getWeakRefHandler().sendEmptyMessage(NULL_HDL1);
							}
						}
						break;
					default:
						break;
					}
				}
			}

		};
	}

	/**
	 * 箱接口查询模型转换
	 */
	private Message setRequestMsg(int pageIndex, long lastId) {
		HistoryRecordsRequest historyRecordsRequest = new HistoryRecordsRequest();
		//historyRecordsRequest.setUserId(loginResult.getUserinfo().getPersonid());
		//historyRecordsRequest.setEnterpriseId(loginResult.getEnterpriseinfo().getEnterpriseid());
		historyRecordsRequest.setPageIndex(pageIndex);
		historyRecordsRequest.setType(1);
		String json = Utils.toJSON(historyRecordsRequest);
		Message message = new Message(PalUtils.MsgType.HistoryRecords, PalUtils.ENCRYPT, json);
		return message;
	}

	/**
	 * 盒接口查询模型转换
	 */
	private Message setRequestMsg1(int pageIndex, long lastId) {
		HistoryRecordsRequest historyRecordsRequest = new HistoryRecordsRequest();
		//historyRecordsRequest.setUserId(loginResult.getUserinfo().getPersonid());
		//historyRecordsRequest.setEnterpriseId(loginResult.getEnterpriseinfo().getEnterpriseid());
		historyRecordsRequest.setPageIndex(pageIndex);
		historyRecordsRequest.setType(2);
		String json = Utils.toJSON(historyRecordsRequest);
		Message message = new Message(PalUtils.MsgType.HistoryRecords, PalUtils.ENCRYPT, json);
		return message;
	}

	/**
	 * 箱盒接口查询结果转换
	 */
	private List<BoxCode> setResponseModelList(String response) {
		HistoryRecordsResult historyRecordsResult = (HistoryRecordsResult) PalUtils.getPostResult(response,
				HistoryRecordsResult.class);
		if (historyRecordsResult != null && historyRecordsResult.getResult() > 0) {
			return historyRecordsResult.getRecordList();
		} else {
			return null;
		}
	}

	private long setLastItemId(Object itemModel) {
		return 0;// setRequestMsg(int pageIndex, long lastId)没用到 所以直接返回0
	}

	/**
	 * 联网获取列表
	 *
	 * @param pageIndex
	 * @param lastId
	 */
	protected void requestList(int pageIndex, long lastId) {
		WeakRefAsyncTask.InterActionModel interActionModel = new WeakRefAsyncTask.InterActionModel();
		interActionModel.setIndex(pageIndex == FIRST_PAGE_INDEX ? REFRESH_RQT : LOAD_MORE_RQT);
		interActionModel.setUrl(getString(R.string.first_url) + getString(R.string.first_url_page));
		interActionModel.setMessage(setRequestMsg(pageIndex, lastId));
		weakRefAsyncTask.execute(interActionModel);
	}

	/**
	 * 联网获取列表
	 *
	 * @param pageIndex
	 * @param lastId
	 */
	protected void requestList1(int pageIndex, long lastId) {
		WeakRefAsyncTask.InterActionModel interActionModel = new WeakRefAsyncTask.InterActionModel();
		interActionModel.setIndex(pageIndex == FIRST_PAGE_INDEX1 ? REFRESH_RQT1 : LOAD_MORE_RQT1);
		interActionModel.setUrl(getString(R.string.first_url) + getString(R.string.first_url_page));
		interActionModel.setMessage(setRequestMsg1(pageIndex, lastId));
		weakRefAsyncTask1.execute(interActionModel);
	}

	/**
	 * 重新刷新界面
	 */
	protected void reload(int selectedIndex) {
		if (selectedIndex == 0) {
			requestList(FIRST_PAGE_INDEX, 0);
			loadingLayout.showLoading();
		} else if (selectedIndex == 1) {
			requestList1(FIRST_PAGE_INDEX1, 0);
			loadingLayout1.showLoading();
		}
	}

	/**
	 * header和footer复位
	 */
	private void onLoad(int selectedIndex) {
		if (selectedIndex == 0) {
			if (xListView == null) {
				return;
			}
			xListView.stopRefresh();
			xListView.stopLoadMore();
			// 是最后一页，则不能加载更多
			if (isLastPage == 1) {
				xListView.setPullLoadEnable(false);
			} else {
				xListView.setPullLoadEnable(true);
			}
		} else if (selectedIndex == 1) {
			if (xListView1 == null) {
				return;
			}
			xListView1.stopRefresh();
			xListView1.stopLoadMore();
			// 是最后一页，则不能加载更多
			if (isLastPage1 == 1) {
				xListView1.setPullLoadEnable(false);
			} else {
				xListView1.setPullLoadEnable(true);
			}
		}

	}
}
